package com.example.cardgame
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

//跟test_third配套  针对不需要保存文件的用法
class HomeActivity : AppCompatActivity() {

    // 声明 ActivityResultLauncher
    private lateinit var editResultLauncher: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.test_first)
        var username:TextView=findViewById(R.id.username)
        var sno:TextView=findViewById(R.id.sno)
        var telephone:TextView=findViewById(R.id.telephone)
        var email:TextView=findViewById(R.id.email)
        // 初始化 ActivityResultLauncher，用于处理返回的数据
        editResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            //修改个人信息
            if (result.resultCode == 6) {
                val data = result.data
                data?.let {
                    // 获取用户信息
                    val userInfo = it.getStringArrayExtra("userInfo")
                    userInfo?.let { info ->
                        val username = findViewById<TextView>(R.id.username)
                        val sno = findViewById<TextView>(R.id.sno)
                        val telephone = findViewById<TextView>(R.id.telephone)
                        val email = findViewById<TextView>(R.id.email)
                        val gender = findViewById<TextView>(R.id.gender)
                        val hobbies = findViewById<TextView>(R.id.hobby)

                        // 设置用户信息
                        if (!info[0].isNullOrEmpty()) {
                            username.text = info[0]
                        }
                        if (!info[1].isNullOrEmpty()) {
                            sno.text = info[1]
                        }
                        if (!info[2].isNullOrEmpty()) {
                            telephone.text = info[2]
                        }
                        if (!info[3].isNullOrEmpty()) {
                            email.text = info[3]
                        }
                        if (!info[4].isNullOrEmpty()) {
                            gender.text = info[4]
                        }
                        if (info.drop(6).any { it.isNotEmpty() }) {
                            hobbies.text = info.drop(5).joinToString(", ")
                        }
                    }

                    // 处理传递的 Bitmap 图像
                    val capturedImage = it.getParcelableExtra<Bitmap>("capturedImage")
                    capturedImage?.let { bitmap ->
                        val enhancedBitmap = enhanceBitmapQuality(bitmap)  // 提升图像质量
                        val imageView = findViewById<ImageView>(R.id.imageView)
                        imageView.setImageBitmap(enhancedBitmap)  // 设置高清化后的图像
                    } ?: run {
                        Toast.makeText(this, "未获取到图片", Toast.LENGTH_SHORT).show()
                    }


                }
            }


            //处理游戏最高分
            else if (result.resultCode == 2) {
                val highScore = findViewById<TextView>(R.id.highScore)
                val tempScore = result.data?.getIntExtra(FIRST_MSG, 0) // 默认值为 0
                val currentHighScore = highScore.text.toString().toIntOrNull() ?: 0
                if (tempScore != null && tempScore > currentHighScore) {
                    highScore.text = tempScore.toString() // 更新最高分数
                }
            }
        }


        //修改个人信息
        val edit: Button = findViewById(R.id.edit)
        edit.setOnClickListener {
            //val intent = Intent(this, test_second::class.java)
            val intent=Intent(this,test_third::class.java)
            intent.putExtra("username", username.text.toString())
            intent.putExtra("sno", sno.text.toString())
            intent.putExtra("phoneNumber", telephone.text.toString())
            intent.putExtra("email", email.text.toString())
            editResultLauncher.launch(intent)
        }
        //翻牌游戏
        val game: Button = findViewById(R.id.game)
        game.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            editResultLauncher.launch(intent)
        }
    }


    private fun enhanceBitmapQuality(bitmap: Bitmap): Bitmap {
        // 提升图片分辨率，这里将宽度和高度放大2倍
        val width = bitmap.width
        val height = bitmap.height
        return Bitmap.createScaledBitmap(bitmap, width * 2, height * 2, true)
    }



}
